623B - Array GCD - CodeForces Solution


dp greedy number theory *2300

Please click on ads to support us..

C++ Code:

/* In the name of God */

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <bitset>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <list>
#include <map>
#include <numeric>
#include <limits>
#include <limits.h>
#include <unordered_map>
#include <unordered_set>
#include <chrono>
#include <random>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VL;
#define PB push_back
#define POP pop_back
#define MP make_pair
#define all(a) (a).begin(), (a).end()
#define endl '\n'
#define dbg(x) cerr << '[' << #x << ": " << x << "]\n"
#define dbg2(x, y) cerr << '[' << #x << ": " << x << ", " << #y << ": " << y << "]\n"
#define Yes cout << "Yes\n"
#define YES cout << "YES\n"
#define No cout << "No\n"
#define NO cout << "NO\n"

const ll INF = (ll)2e18 + 1386;
const ld EPS = 0.000000000000001;
const int MOD = 1e9 + 7;

inline int mod_add(int a, int b){ int res = a + b; return (res >= MOD? res - MOD : res); }
inline int mod_neg(int a, int b){ int res = (abs(a - b) < MOD? a - b : (a - b) % MOD); return (res < 0? res + MOD : res); }
inline int mod_mlt(int a, int b){ return (1ll * a * b % MOD); }
inline string intToString(ll a){ char x[100]; sprintf(x, "%lld", a); string s = x; return s; }
inline ll stringToInt(string s){ ll res; char x[100]; strcpy(x, s.c_str()); sscanf(x, "%lld", &res); return res; }
inline void fileIO(string i, string o){ freopen(i.c_str(), "r", stdin); freopen(o.c_str(), "w", stdout); }

const int MAXN = 1e6 + 16;

int n, A, B, a[MAXN];
ll dp[MAXN][3];

void init(){
    for (int i = 1; i <= n; i++){
        dp[i][0] = dp[i][1] = dp[i][2] = INF;
    }
}

VI primes(int x){
    int tmp = x;
    VI res;
    for (int i = 2; i * i <= x; i++){
        if (tmp % i == 0){
            while (tmp % i == 0) tmp /= i;
            res.PB(i);
        }
    }
    if (tmp > 1) res.PB(tmp);
    return res;
}

bool check(int x, int p){
    return ((x - 1) % p == 0 || x % p == 0 || (x + 1) % p == 0);
}

ll ansfor(int p){
    dp[1][0] = 0;
    if (a[1] % p) dp[1][0] = B;
    for (int i = 2; i <= n; i++){
        bool ck = check(a[i], p);
        if (ck) dp[i][0] = min(dp[i][0], dp[i - 1][0] + B * (a[i] % p != 0)); // oops! was boolean :(
        dp[i][1] = min(dp[i][1], min(dp[i - 1][0], dp[i - 1][1]) + A);
        if (ck) dp[i][2] = min(dp[i][2], min(dp[i - 1][1], dp[i - 1][2]) + B * (a[i] % p != 0));
    }
    return min({dp[n][0], dp[n][1], dp[n][2]});
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n >> A >> B;
    for (int i = 1; i <= n; i++){
        cin >> a[i];
    }
    ll ans = INF;
    for (int it = 0; it < 2; it++){
        for (int delta = -1; delta <= 1; delta++){
            for (int p : primes(a[1] + delta)){
                init();
                ans = min(ans, ansfor(p));
            }
        }
        reverse(a + 1, a + n + 1);
    }
    cout << ans;
    return 0;
}


Comments

Submit
0 Comments
More Questions

1473A - Replacing Elements
959A - Mahmoud and Ehab and the even-odd game
78B - Easter Eggs
1455B - Jumps
1225C - p-binary
1525D - Armchairs
1257A - Two Rival Students
1415A - Prison Break
1271A - Suits
259B - Little Elephant and Magic Square
1389A - LCM Problem
778A - String Game
1382A - Common Subsequence
1512D - Corrupted Array
667B - Coat of Anticubism
284B - Cows and Poker Game
1666D - Deletive Editing
1433D - Districts Connection
2B - The least round way
1324A - Yet Another Tetris Problem
246B - Increase and Decrease
22E - Scheme
1566A - Median Maximization
1278A - Shuffle Hashing
1666F - Fancy Stack
1354A - Alarm Clock
1543B - Customising the Track
1337A - Ichihime and Triangle
1366A - Shovels and Swords
919A - Supermarket